"""
wow.core
========

Information to follow.

"""

armory_url = 'http://%(region)s.wowarmory.com/%(action)s.xml?%(query)s'
fake_user_agent = 'Firefox/3.0'
regions = ('eu', 'us')  # todo: add support for other localisations
replace_keys = {'critHitPercent': 'crit_percent', 'healthRegen': 'health_regen', 'manaRegen': 'mana_regen', 'petBonus': 'pet_bonus', 'mainHandDamage': 'main_hand_damage', 'offHandDamage': 'off_hand_damage', 'mainHandSpeed': 'main_hand_speed', 'offHandSpeed': 'off_hand_speed', 'hitRating': 'hit_rating', 'critChance': 'crit_chance', 'notCasting': 'not_casting', 'increasedHitPercent': 'increased_hit_percent', 'bonusDamage': 'bonus_damage', 'fromType': 'from_type', 'bonusHealing': 'bonus_healing', 'increasePercent': 'increase_percent', 'hitPercent': 'hit_percent', 'damagePercent': 'damage_percent'}

class ArmoryInterface(object):
    """
    ArmoryInterface
    ===============
    
    A basic Armory interface, which takes care of... um, nothing
    currently.
    
    """
    def __init__(self, api=None, region=None):
        """
        __init__(self, api=None, region=None) -> ArmoryInterface
        Initialise the interface instance. Assuming an interface
        follows the quasi-standard, it is recommended that this
        class instantiator be called.
        
        """
        self.api = api
        self.region = region
    
    def assert_api(self, method=None):
        """
        _assert_api(self, method=None) -> bool
        Asserts that the interface object is bound to a ``wow.API``
        instance; raises an exception otherwise.
        
        """
        assert self.api, AttributeError('%s is not bound to a ``wow.API`` instance, and therefore ``%s`` cannot be called.' % (repr(self), method))

class BasicContainer(dict):
    """
    BasicContainer
    ==============
    
    A simple container class, which essentially is meant to work as
    a namespace for separation of various parts of Armory results.
    Implemented as a tiny extension of ``dict``.
    
    """
    
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

class RecursionContainer(BasicContainer):
    """
    RecursionContainer
    ==================
    
    A container that does some of the work for you (yay!) -- this
    will do its best to recurse through an XML tree and store the
    data in a logical way.
    
    """
    def __init__(self, element):
        """
        RecursionContainer.__init__(self, element) -> RecursionContainer
        
        """
        self.update(self.recurse(element))
    
    def recurse(self, element):
        """
        recurse(self, element) -> dict
        
        """
        stuff = BasicContainer()
        for attribute in element._dir:
            try:
                values = BasicContainer()
                for key, value in attribute._attrs.items():
                    key = replace_keys.get(key, key)
                    try:
                        values[key] = int(value)
                    except ValueError:
                        try:
                            values[key] = float(value)
                        except ValueError:
                            values[key] = value
                if attribute._dir:
                    add = self.recurse(attribute)
                    try:
                        values.update(add)
                    except ValueError:
                        try:
                            values = int(add)
                        except ValueError:
                            try:
                                values = float(add)
                            except ValueError:
                                values = add
                stuff[replace_keys.get(attribute._name, attribute._name)] = values
            except AttributeError:
                stuff = attribute
        return stuff
